//
//  UIDevice+Util.m
//  SWIOSLibary
//
//  Created by shaowei on 13-7-25.
//  Copyright (c) 2013年 LianZhan. All rights reserved.
//

#import "UIDevice+Util.h"
#include <sys/sysctl.h>

@implementation UIDevice (Util)

static NSDictionary *dic = nil;

+ (NSString*)getDeviceModelInfo{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"DeviceModelInfo" ofType:@"plist"];

        if (!path) {
#define SWDeviceErrorPromptInfo @"DeviceModelInfo.plist 文件不存在，请添加此文件"
#ifdef SWASSERT
            SWASSERT(0, SWDeviceErrorPromptInfo);
#else
            NSLog(SWDeviceErrorPromptInfo);
#endif
        }
        

        dic = [[NSDictionary dictionaryWithContentsOfFile:path] copy];
    });
    
    size_t size;
    int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    
    NSString *device = dic[platform];
    
    return device?device:platform;
}


//#import <sys/utsname.h>
//
//NSString* machineName()
//{
//    struct utsname systemInfo;
//    uname(&systemInfo);
//    
//    return [NSString stringWithCString:systemInfo.machine
//                              encoding:NSUTF8StringEncoding];
//}

@end
